#ifndef __COMMH_CLASSDB_H__
#define __COMMH_CLASSDB_H__

#include "commclass.h"
#include "CTCodeMySQL.h"

using namespace CTLib;

#ifndef CHECK_ENCODE_RET
#define CHECK_ENCODE_RET(iRet) \
    if (iRet) \
    { \
        CT_WARNING(("MysqlEncodeStruct fail: ret=%d", iRet)); \
        return iRet; \
    }
#endif

//////////////////////////////////////////////////////////////////////////
class CPlayerBaseInfoDB : public CTCodeObject
{
public:
    static int BuildSelectBriefSQL(CReqSQL& stReq, const char* szDBName, uint64 u64OffSet)
    {
        stReq.Set("select Brief from %s.TPlayerBaseInfo limit %llu, %d", szDBName, u64OffSet,
            SELECT_PLAYER_BASE_BRIEF_COUNT_PER_TIME);
        return 0;
    }

    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, uint64 u64GID)
    {
        stReq.Set("select Data from %s.TPlayerBaseInfo where GID = %llu", szDBName, u64GID);
        return 0;
    }

    static int BuildSelectGIDSQL(CReqSQL& stReq, const char* szDBName, CTSizeString<USER_TOKEN_LEN>& szUserToken)
    {
        char sBuf[1024] = {0};

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf), szUserToken.Get(), szUserToken.GetDataLen());

        stReq.Set("select GID from %s.TPlayerBaseInfo where UserToken = '%s'", szDBName, sBuf);

        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CPlayerBaseInfo& stPlayerBaseInfo,
        CTBuffer& stEncodeBuf)
    {
        int iRet = 0;
        char sBuf[1024] = {0};

        stReq.Set("update %s.TPlayerBaseInfo set ", szDBName);

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf), stPlayerBaseInfo._stBriefInfo._sUserToken.Get(),
            stPlayerBaseInfo._stBriefInfo._sUserToken.GetDataLen());

        stReq.Cat("UserToken = '%s', ", stPlayerBaseInfo._stBriefInfo._sUserToken());

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf), stPlayerBaseInfo._stBriefInfo._sRoleName.Get(),
            stPlayerBaseInfo._stBriefInfo._sRoleName.GetDataLen());

        stReq.Cat("RoleName = '%s', ", sBuf);

        stReq.Cat("Gold = %d, ", stPlayerBaseInfo._iGold);
        stReq.Cat("Silver = %lld, ", stPlayerBaseInfo._iSilver);
        stReq.Cat("RoleLevel = %d, ", stPlayerBaseInfo._shLevel);
        stReq.Cat("PayAmount = %d, ", stPlayerBaseInfo._iPayAmount);

        stReq.Cat("Brief = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo._stBriefInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %llu ", stPlayerBaseInfo._stBriefInfo._u64GID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerBaseInfo& stPlayerBaseInfo,
        CTBuffer& stEncodeBuf)
    {
        int iRet = 0;
        char sBuf[1024] = {0};

        stReq.Set("replace into %s.TPlayerBaseInfo set ", szDBName);
        stReq.Cat("GID = %llu, ", stPlayerBaseInfo._stBriefInfo._u64GID);

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf), stPlayerBaseInfo._stBriefInfo._sUserToken.Get(),
            stPlayerBaseInfo._stBriefInfo._sUserToken.GetDataLen());
        
        stReq.Cat("UserToken = '%s', ", sBuf);

        TMySQLCharset::Instance()->EscapeString(sBuf, sizeof(sBuf), stPlayerBaseInfo._stBriefInfo._sRoleName.Get(),
            stPlayerBaseInfo._stBriefInfo._sRoleName.GetDataLen());

        stReq.Cat("RoleName = '%s', ", sBuf);

        stReq.Cat("Gold = %d, ", stPlayerBaseInfo._iGold);
        stReq.Cat("Silver = %lld, ", stPlayerBaseInfo._iSilver);
        stReq.Cat("RoleLevel = %d, ", stPlayerBaseInfo._shLevel);
        stReq.Cat("PayAmount = %d, ", stPlayerBaseInfo._iPayAmount);

        stReq.Cat("Brief = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo._stBriefInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("', ");

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerBaseInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

    static int BuildSelectMaxGIDSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select ifnull(max(GID %% %llu), 0) from %s.TPlayerBaseInfo", MAX_USER_PER_ZONE, szDBName);
        return 0;
    }

public:
    CPlayerBaseInfo stPlayerBaseInfo;
};

BEGIN_CODE_CONVERT(CPlayerBaseInfoDB)
CODE_CONVERT(stPlayerBaseInfo)
END_CODE_CONVERT(CPlayerBaseInfoDB)

//////////////////////////////////////////////////////////////////////////
class CPlayerDetailInfoDB : public CTCodeObject
{
public:
    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, uint64 u64GID)
    {
        stReq.Set("select Data from %s.TPlayerDetailInfo where GID = %llu", szDBName, u64GID);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CPlayerDetailInfo& stPlayerDetailInfo,
        CTBuffer& stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("update %s.TPlayerDetailInfo set ", szDBName);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");

        stReq.Cat("where GID = %llu ", stPlayerDetailInfo._u64GID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CPlayerDetailInfo& stPlayerDetailInfo,
        CTBuffer& stEncodeBuf)
    {
        int iRet = 0;

        stReq.Set("replace into %s.TPlayerDetailInfo set ", szDBName);
        stReq.Cat("GID = %llu, ", stPlayerDetailInfo._u64GID);

        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stPlayerDetailInfo, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

public:
    CPlayerDetailInfo stPlayerDetailInfo;
};

BEGIN_CODE_CONVERT(CPlayerDetailInfoDB)
CODE_CONVERT(stPlayerDetailInfo)
END_CODE_CONVERT(CPlayerDetailInfoDB)

//////////////////////////////////////////////////////////////////////////
class CZoneDataDB : public CTCodeObject
{
public:
    static int BuildSelectSQL(CReqSQL& stReq, const char* szDBName, int iDataID)
    {
        stReq.Set("select Data from %s.TZoneData where DataID = %d", szDBName, iDataID);
        return 0;
    }

    static int BuildSelectCountSQL(CReqSQL& stReq, const char* szDBName)
    {
        stReq.Set("select count(*) from %s.TZoneData", szDBName);
        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CZoneBaseData& stBaseData, CTBuffer& stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("update %s.TZoneData set ", szDBName);
        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stBaseData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");
        stReq.Cat("where DataID = %d ", stBaseData._iDataID);

        return 0;
    }

    static int BuildUpdateSQL(CReqSQL& stReq, const char* szDBName, CZoneDetailData& stDetailData,
        CTBuffer& stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("update %s.TZoneData set ", szDBName);
        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stDetailData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("' ");
        stReq.Cat("where DataID = %d ", stDetailData._iDataID);

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CZoneBaseData& stBaseData, CTBuffer& stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("replace into %s.TZoneData set ", szDBName);
        stReq.Cat("DataID = %d, ", stBaseData._iDataID);
        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stBaseData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }

    static int BuildReplaceSQL(CReqSQL& stReq, const char* szDBName, CZoneDetailData& stDetailData,
        CTBuffer& stEncodeBuf)
    {
        int iRet = 0;
        stReq.Set("replace into %s.TZoneData set ", szDBName);
        stReq.Cat("DataID = %d, ", stDetailData._iDataID);
        stReq.Cat("Data = '");
        iRet = MysqlEncodeStruct(stReq.stSQLMen, stDetailData, stEncodeBuf);
        CHECK_ENCODE_RET(iRet);
        stReq.Cat("'");

        return 0;
    }
};

#endif //__COMMH_CLASSDB_H__

